package com.mall4j.cloud.product.controller.admin;

import com.mall4j.cloud.api.product.dto.SpuUpdateDTO;
import com.mall4j.cloud.api.product.vo.*;
import com.mall4j.cloud.common.constant.StatusEnum;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;
import com.mall4j.cloud.product.dto.SpuDTO;
import com.mall4j.cloud.product.model.*;
import com.mall4j.cloud.product.service.*;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * spu
 *
 * @author JCM
 * @date 2021-04-27 15:33:00
 */
@RestController("adminSpuController")
@RequestMapping("/admin/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SpuAttrValueService spuAttrValueService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuSkuAttrValueService spuSkuAttrValueService;

    @Autowired
    private SpuExtensionService spuExtensionService;

    @Autowired
    private MapperFacade mapperFacade;


    @Transactional
    @PostMapping
    @ApiOperation(value = "保存spu", notes = "保存spu")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SpuDTO spuDTO) {
        Spu spu = mapperFacade.map(spuDTO, Spu.class);
        spu.setStatus(StatusEnum.ENABLE.value());
        spu.setShopId(AuthUserContext.get().getTenantId());
        spuService.save(spu);

        //保存其它信息到相关联的表

        //保存商品规格属性关联信息
        List<SpuAttrValueVO> spuAttrValues = spuDTO.getSpuAttrValues();
        for (SpuAttrValueVO sav : spuAttrValues) {
            SpuAttrValue spuAttrValue = mapperFacade.map(sav, SpuAttrValue.class);
            spuAttrValue.setSpuId(spu.getSpuId());
            spuAttrValueService.save(spuAttrValue);
        }

        //保存商品详情信息
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpuId(spu.getSpuId());
        spuDetail.setDetail(spuDTO.getDetail());
        spuDetailService.save(spuDetail);

        //保存sku信息
        List<SkuVO> skus = spuDTO.getSkuList();
        for (SkuVO skuVO : skus) {
            Sku sku = mapperFacade.map(skuVO, Sku.class);
            sku.setStatus(StatusEnum.ENABLE.value());
            sku.setSpuId(spu.getSpuId());
            skuService.save(sku);

            //保存销售属性信息
            List<SpuSkuAttrValueVO> spuSkuAttrValues = skuVO.getSpuSkuAttrValues();
            for (SpuSkuAttrValueVO spuSkuAttrValue : spuSkuAttrValues) {
                SpuSkuAttrValue skuAttrValue = mapperFacade.map(spuSkuAttrValue, SpuSkuAttrValue.class);
                skuAttrValue.setSpuId(spu.getSpuId());
                skuAttrValue.setSkuId(sku.getSkuId());
                skuAttrValue.setStatus(StatusEnum.ENABLE.value());
                spuSkuAttrValueService.save(skuAttrValue);
            }

            //保存库存信息
            SpuExtension spuExtension = new SpuExtension();
            spuExtension.setSpuId(spu.getSpuId());
            spuExtension.setSaleNum(0);
            spuExtension.setActualStock(spuDTO.getTotalStock());
            spuExtension.setStock(spuDTO.getTotalStock());
            spuExtension.setLockStock(0);
            spuExtensionService.save(spuExtension);

        }
        return ServerResponseEntity.success();
    }

    @Transactional
    @PutMapping
    @ApiOperation(value = "更新spu",notes = "更新spu")
    public ServerResponseEntity<Void> update(@RequestParam SpuDTO spuDTO){
        Spu spu = mapperFacade.map(spuDTO, Spu.class);
        spuService.update(spu);

        //更新关联表中的数据
        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpuId(spuDTO.getSpuId());
        spuDetail.setDetail(spuDTO.getDetail());
        spuDetailService.update(spuDetail);

        SpuExtension spuExtension = new SpuExtension();
        spuExtension.setSpuId(spuDTO.getSpuId());
        spuExtension.setActualStock(spuDTO.getTotalStock());
        spuExtension.setStock(spuDTO.getTotalStock());
        spuExtensionService.update(spuExtension);

        //删除spu相关属性重新添加
        spuAttrValueService.deleteBySpuId(spuDTO.getSpuId());
        //保存商品规格属性关联信息
        List<SpuAttrValueVO> spuAttrValues = spuDTO.getSpuAttrValues();
        for (SpuAttrValueVO sav : spuAttrValues) {
            SpuAttrValue spuAttrValue = mapperFacade.map(sav, SpuAttrValue.class);
            spuAttrValue.setSpuId(spu.getSpuId());
            spuAttrValueService.save(spuAttrValue);
        }

        //直接删除sku和其相关属性重新添加
        skuService.deleteBySpuId(spuDTO.getSpuId());
        spuSkuAttrValueService.deleteBySpuId(spuDTO.getSpuId());
        //保存sku信息
        List<SkuVO> skus = spuDTO.getSkuList();
        for (SkuVO skuVO : skus) {
            Sku sku = mapperFacade.map(skuVO, Sku.class);
            sku.setStatus(StatusEnum.ENABLE.value());
            sku.setSpuId(spu.getSpuId());
            skuService.save(sku);

            //保存销售属性信息
            List<SpuSkuAttrValueVO> spuSkuAttrValues = skuVO.getSpuSkuAttrValues();
            for (SpuSkuAttrValueVO spuSkuAttrValue : spuSkuAttrValues) {
                SpuSkuAttrValue skuAttrValue = mapperFacade.map(spuSkuAttrValue, SpuSkuAttrValue.class);
                skuAttrValue.setSpuId(spu.getSpuId());
                skuAttrValue.setSkuId(sku.getSkuId());
                skuAttrValue.setStatus(StatusEnum.ENABLE.value());
                spuSkuAttrValueService.save(skuAttrValue);
            }
        }

        return ServerResponseEntity.success();
    }

    @PutMapping("/prod_status")
    @ApiOperation(value = "商品上下架",notes = "商品上下架")
    public ServerResponseEntity<Void> prodStatus(SpuDTO spuDTO){
        SpuVO spuVO = spuService.getBySpuId(spuDTO.getSpuId());
        Spu spu = mapperFacade.map(spuVO, Spu.class);
        spu.setStatus(spuDTO.getStatus());
        spuService.update(spu);
        return ServerResponseEntity.success();
    }


    @DeleteMapping
    @ApiOperation(value = "删除spu", notes = "根据spuId删除spu")
    public ServerResponseEntity<Void> delete(@RequestParam Long spuId) {
        spuService.deleteById(spuId);
        return ServerResponseEntity.success();
    }
}
